package gui;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;

import client.ClientConsole;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Shows a guest user window, enables access for login and search.
 */
public class GuestWindow extends JPanel implements ActionListener{

	private static final long serialVersionUID = -4045986246419252610L;
	private JPanel mainPanel,searchPanel,optionsPanel,radioPanel,topPanel,searchTextPanel;
	private JTextField bookSearch;
	private JButton search;
	private JButton login;
	/** radio Button for search selection*/
	JRadioButtonMenuItem  bookName,bookAuthor,freeText,subject;
	/** Container for radio buttons*/  
	ButtonGroup bGroup = new ButtonGroup();
	
	/**Default constructor- creates the  guest window*/
	public GuestWindow() {
		super();
		setSize(300, 300);
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		bookSearch=new JTextField(20);
		search= new JButton("Search");
		search.addActionListener(this);
		topPanel=new JPanel();
		topPanel.setLayout(new BorderLayout());
		searchPanel=new JPanel();
		searchPanel.setLayout( new BoxLayout( searchPanel, BoxLayout.Y_AXIS ) );
		searchTextPanel=new JPanel();
		radioPanel= new JPanel();
		searchTextPanel.add(bookSearch);
		searchTextPanel.add(search);
		bookName=new JRadioButtonMenuItem ("Book name");
		subject=new JRadioButtonMenuItem ("Subject");
		bookAuthor=new JRadioButtonMenuItem ("Author");
		freeText=new JRadioButtonMenuItem ("Free Text");
		bGroup.add(bookName);
		bGroup.add(bookAuthor);
		bGroup.add(subject);
		bGroup.add(freeText);
		radioPanel.add(bookName);
		radioPanel.add(bookAuthor);
		bookName.setSelected(true);
		radioPanel.add(subject);
		radioPanel.add(freeText);
		searchPanel.add(searchTextPanel);
		searchPanel.add(radioPanel);
		mainPanel.add(searchPanel,BorderLayout.NORTH);
		optionsPanel=new JPanel();
		login=new JButton();
		login.setIcon(new ImageIcon("C:\\images\\login.png"));
		login.setContentAreaFilled(false);
		login.setBorderPainted(false);
		login.addActionListener(this);
		optionsPanel.add(login);
		mainPanel.add(optionsPanel,BorderLayout.CENTER);
		add(mainPanel);
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if( ae.getSource() == login )
		{
			LoginForm lg=new LoginForm();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(lg);
		}
		else if (ae.getSource()==search)
		{
			if (!bookSearch.getText().isEmpty())
			{
				String query=bookSearch.getText();
				String option = "";
				
				if (bookName.isSelected())
					option="Book Name";
				else if (bookAuthor.isSelected())
					option="Author";
				else if (subject.isSelected())
					option="Subject";
				else if (freeText.isSelected())
					option="Free";
				this.setVisible(false);
				ClientConsole.aFrame.setContentPane(new SearchForm(query,option));
			}
		}
		
	}
}
